<?php
class UserAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        // if (!in_array(ACTION_NAME, array('edit', 'update'))) {
            // $Config->checkUserLevel(array('1'));
        // } else {
            // $Config->checkUserLevel(array('1', '2', '3'));
        // }
        $Config->checkUserLevel(array('1'));
    }
    
    public function index() {
        $User = M('User');
        import("ORG.Util.Page");
        $count = $User->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page',$show);
        
        $user = $User->order(array('user_id' => 'asc'))->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('user', $user);
        $this->display();
    }
    
    public function add() {
        $UserType = M('UserType');
        $user_type = $UserType->order(array('user_type_id' => 'asc'))->select();
        
        $this->assign('user_type', $user_type);
        $this->display();
    }
    
    public function insert() {
        if (isset($_POST['submitted'])) {
            $User = D('User');
            if ($User->create()) {
                if ($User->add()) {
                    $this->success('成功添加用户！');
                } else {
                    $this->error('添加用户失败！');
                }
            } else {
                $this->error($User->getError());
            }
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $User = M('User');
            $user = $User->find($id);
            
            $UserType = M('UserType');
            $user_type = $UserType->order(array('user_type_id' => 'asc'))->select();
            
            $this->assign('user', $user);
            $this->assign('user_type', $user_type);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function update() {
        if (isset($_POST['submitted'])) {
            $User = D('User');
            
            $password = $_POST['password'];
            if ($password == '') {
                $validate = array(
                    array('user_name', 'checkUserName', '用户名必须为3-14个字节！', 0, 'callback'),
                    array('user_type_id', array(1, 2, 3), '请选择用户类型！', 2, 'in')
                );
                $User->setProperty("_validate", $validate);
                
                $auto = array ();
                $User->setProperty("_auto", $auto);
            }
            
            if ($user = $User->create()) {
                // unset($user['password']);
                if (false !== $User->save($user)) {
                    $this->success('修改成功！');
                } else {
                    $this->error('修改失败！');
                }
            } else {
                $this->error($User->getError());
            }
        }
    }
    
    public function delete() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $User = M('User');
            if ($User->delete($id)) {
                $this->success('成功删除用户！');
            } else {
                $this->error('删除用户失败！');
            }
        }
    }
    
    // 登陆
    public function login() {
        // 传配置
        $Setting = M('Setting');
        $setting = $Setting->find(1);
        
        // $User = M('User');
        // $user = $User->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->find();
        
        $this->assign('site_name', $setting['site_name']);
        
        // 判断是否已经登陆
        if (isset($_SESSION['user']) && in_array($_SESSION['user']['user_type_id'], array('1', '2'))) {
            $this->assign('jumpUrl', __APP__);
            $this->success('您已登陆成功！');
        }
        
        if (isset($_POST['submitted'])) {
        
            if($_SESSION['verify'] != md5($_POST['verify'])) {
                $this->error('验证码错误！');
            }
        
            $user_data = array();
            $user_data['user_name'] = mysql_real_escape_string($_POST['user_name']);
            $user_data['password'] = sha1(mysql_real_escape_string($_POST['password']));
            
            $User = M('User');
            if ($user = $User->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->where($user_data)->find()) {
                if (!in_array($user['user_type_id'], array('1', '2'))) {
                    $this->error('请到教师系统登录！');
                }
                $_SESSION['user'] = $user;
                $this->assign('jumpUrl', __APP__);
                $this->success('登陆成功，现在为您转向后台管理！');
            } else {
                $this->error('用户名或者密码错误！');
            }
        }
        $this->display();
    }
    
    // 退出登陆
    public function logout() {
        if (isset($_SESSION['user'])) {
            if ($_SESSION['user']['user_type_id'] == 3) {
                $jumpUrl = U('TeacherSystem/login');
            } else {
                $jumpUrl = U('User/login');
            }
            unset($_SESSION['user']);
            session_destroy();
        } else {
            $jumpUrl = __ROOT__;
        }
        
        $this->assign('jumpUrl', $jumpUrl);
        $this->success('成功退出登录！');
    }
    
}
?>